package com.ross.web.transaction;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.ServletActionContext;
import org.hibernate.criterion.Order;

import com.opensymphony.xwork2.ActionSupport;
import com.ross.web.DTO.ProductDTO;
import com.ross.web.dao.AuditLogDAO;
import com.ross.web.dao.OrdersDAO;
import com.ross.web.domain.AuditLog;
import com.ross.web.domain.Orders;
import com.ross.web.domain.Users;
import com.ross.web.util.DateUtil;
import com.ross.web.util.SessionLogout;

public class OrdersAction extends ActionSupport {
	
	private List<Orders> ordersList;
	private String ordersanswer;
	private int oid;
	private Orders orders;
	private int dispMode;
	private List<ProductDTO> productList = new ArrayList<ProductDTO>();
	
	private SessionLogout sessionLogout = new SessionLogout();
	private HttpServletRequest request = ServletActionContext.getRequest();
	
	OrdersDAO ordersDAO = new OrdersDAO();
	
	public String goToOrdersAction(){
		
		if (sessionLogout.SessionExpired()) {
			addActionMessage("Session already expired");
			return "sessionExpired";
		}
		
		setDispMode(1);
		setOrdersanswer("NO");
		ordersList = ordersDAO.getAllOrders("NO");
		return SUCCESS;
	}
	
	public String doGetOrdersAction(){
		
		if (sessionLogout.SessionExpired()) {
			addActionMessage("Session already expired");
			return "sessionExpired";
		}
		
		setDispMode(1);
		System.out.println(getOrdersanswer());
		setOrdersanswer(getOrdersanswer());
		ordersList = ordersDAO.getAllOrders(getOrdersanswer());
		return SUCCESS;
		
	}
	
	public String confirmOrderAction(){
		if (sessionLogout.SessionExpired()) {
			addActionMessage("Session already expired");
			return "sessionExpired";
		}
		
		try {
			
			orders = ordersDAO.getOrdersById(oid);
			orders.setConfirmdate(DateUtil.getDateToday());
			orders.setConfirmtime(DateUtil.getTimeToday());
			orders.setIsconfirmed("YES");
			
			ordersDAO.update(orders);
			addActionMessage(getText("success.order.confirmed"));
			
			Users ua = (Users) request.getSession().getAttribute("userAccess");
			
			AuditLog al = new AuditLog();
			
			al.setAl_log_date(DateUtil.getDateToday());
			al.setAl_log_time(DateUtil.getTimeToday());
			al.setAl_user(ua.getFirstname() + " " + ua.getMiddlename().substring(0,1) + ". " + ua.getLastname());
			al.setAl_grand_total(String.valueOf(orders.getOrdergrandtotal()));
			al.setAl_products(orders.getAllproducts());
			al.setAl_username(ua.getUsername());
			
			AuditLogDAO auditLogDAO = new AuditLogDAO();
			auditLogDAO.insert(al);
			
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		setDispMode(1);
		setOrdersanswer("NO");
		ordersList = ordersDAO.getAllOrders("NO");
		return SUCCESS;
	}
	
	public String goToViewOrderAction(){
		
		if (sessionLogout.SessionExpired()) {
			addActionMessage("Session already expired");
			return "sessionExpired";
		}
		
		try {
			
			orders = ordersDAO.getOrdersById(oid);
			String[] products = orders.getAllproducts().split("-");
			for(int a=1; a < products.length;a+=3){
				try {
					ProductDTO productDTO = new ProductDTO();
					productDTO.setName(products[a]);
					productDTO.setQuantity(Integer.valueOf(products[a+1]));
					productDTO.setSubTotal(products[a+2]);
					System.out.println(productDTO.getName());
					productList.add(productDTO);
				} catch (IndexOutOfBoundsException e) {
					break;
				}
			}
			
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		setDispMode(2);
		setOrdersanswer(getOrdersanswer());
		return SUCCESS;
	}
	
	
	
	public List<ProductDTO> getProductList() {
		return productList;
	}

	public void setProductList(List<ProductDTO> productList) {
		this.productList = productList;
	}

	public int getDispMode() {
		return dispMode;
	}

	public void setDispMode(int dispMode) {
		this.dispMode = dispMode;
	}

	public Orders getOrders() {
		return orders;
	}

	public void setOrders(Orders orders) {
		this.orders = orders;
	}

	public int getOid() {
		return oid;
	}

	public void setOid(int oid) {
		this.oid = oid;
	}

	public String getOrdersanswer() {
		return ordersanswer;
	}



	public void setOrdersanswer(String ordersanswer) {
		this.ordersanswer = ordersanswer;
	}



	public List<Orders> getOrdersList() {
		return ordersList;
	}

	public void setOrdersList(List<Orders> ordersList) {
		this.ordersList = ordersList;
	}
	
	

}
